package com.cgsd.heyu.utils;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.bun.miitmdid.core.ErrorCode;
import com.bun.miitmdid.core.IIdentifierListener;
import com.bun.miitmdid.core.MdidSdk;
import com.bun.miitmdid.core.MdidSdkHelper;
import com.bun.miitmdid.supplier.IdSupplier;

/**
 * Created by zheng on 2019/8/22.
 */

public class MiitHelper implements IIdentifierListener {

    private AppIdsUpdater _listener;
    public MiitHelper(AppIdsUpdater callback){
        _listener=callback;
    }


    public void getDeviceIds(Context cxt){
        long timeb= System.currentTimeMillis();
        int nres = CallFromReflect(cxt);
        long timee= System.currentTimeMillis();
        long offset=timee-timeb;
        if(nres == ErrorCode.INIT_ERROR_DEVICE_NOSUPPORT){//不支持的设备

        }else if( nres == ErrorCode.INIT_ERROR_LOAD_CONFIGFILE){//加载配置文件出错

        }else if(nres == ErrorCode.INIT_ERROR_MANUFACTURER_NOSUPPORT){//不支持的设备厂商

        }else if(nres == ErrorCode.INIT_ERROR_RESULT_DELAY){//获取接口是异步的，结果会在回调中返回，回调执行的回调可能在工作线程

        }else if(nres == ErrorCode.INIT_HELPER_CALL_ERROR){//反射调用出错

        }
        Log.d(getClass().getSimpleName(),"return value: "+ String.valueOf(nres));
    }

    private int CallFromReflect(Context cxt){
        return MdidSdkHelper.InitSdk(cxt,true,this);
    }

    private int DirectCall(Context cxt){
        MdidSdk sdk = new MdidSdk();
        return sdk.InitSdk(cxt,this);
    }
    @Override
    public void OnSupport(boolean isSupport, IdSupplier _supplier) {
        if(_supplier==null) {
            return;
        }
        String oaid=_supplier.getOAID();
        _supplier.shutDown();
        if(_listener!=null && isSupport){
            _listener.OnIdsAvalid(oaid);
        }
    }
    public interface AppIdsUpdater{
        void OnIdsAvalid(@NonNull String ids);
    }
   public String getOAID(IdSupplier _supplier){
        return _supplier.getOAID();
   }
}
